<style lang="less">
@import "@/styles/table-common.less";
// @import "./companyManage.less";
</style> 
<template>
  <div class="search">
    <Card>
      <Row v-show="openSearch" @keydown.enter.native="handleSearch">
        <Form ref="searchForm" :model="searchForm" inline :label-width="150">
          <!-- <FormItem label="企业ID：">
            <Input
              type="text"
              v-model="searchForm.id"
              clearable
              placeholder="请输入企业ID"
              style="width: 200px"
            />
          </FormItem> -->
          <FormItem label="企业/商铺名称：">
            <Input
              type="text"
              v-model="searchForm.companyName"
              clearable
              placeholder="请输入企业/商铺名称"
              style="width: 200px"
            />
          </FormItem>
          <FormItem label="企业/商铺管理员：">
            <Input
              type="text"
              v-model="searchForm.userNickName"
              clearable
              placeholder="请输入企业/商铺管理员"
              style="width: 200px"
            />
          </FormItem>
          <FormItem label="企业性质：">
            <Select v-model="searchForm.companyTypeId" transfer placeholder="请选择" style="width: 200px">
              <Option
                v-for="item in companyNatureList"
                :value="item.id"
                :key="item.id"
                :label="item.title"
              >{{ item.title }}
              </Option>
            </Select>
          </FormItem>
          <span v-if="drop">
          </span>
          <FormItem style="margin-left: -35px" class="br">
            <Button @click="handleSearch" type="primary" icon="ios-search"
              >搜索</Button
            >
            <Button @click="handleReset">重置</Button>
          </FormItem>
        </Form>
      </Row>
      <Row class="operation">
        <Button @click="add" type="primary" icon="md-add">新建</Button>
      </Row>
      <Table
        :loading="loading"
        border
        :columns="columns"
        :data="data"
        sortable="custom"
        @on-sort-change="changeSort"
        @on-selection-change="showSelect"
        ref="table"
      >
        <template slot-scope="{ row }" slot="nickname">{{ row.user.nickname }}</template>
        <template slot-scope="{ row }" slot="mobile">{{ row.user.mobile }}</template>
      </Table>
      <Row type="flex" justify="end" class="page">
        <Page
          :current="searchForm.pageNumber"
          :total="total"
          :page-size="searchForm.pageSize"
          @on-change="changePage"
          @on-page-size-change="changePageSize"
          :page-size-opts="[10, 20, 50]"
          size="small"
          show-total
          show-elevator
          show-sizer
        ></Page>
      </Row>
    </Card>

    <addEdit
      :data="form"
      :type="showType"
      :companyTypeList="companyTypeList"
      :companyScaleList="companyScaleList"
      :companyNatureList="companyNatureList"
      :idenTypeList="idenTypeList"
      v-model="showUser"
      @on-submit="getDataList"
    />
  </div>
</template>

<script>
import {
  enableUser,
  disableUser,
  deleteUser,

  scCompanyUpdStatusById,
  getCompanyScale, 
  getCompanyType, 
  getScParkRoleAll,
  getFindAll,
  getAllCompany
} from "@/api/index";

import addEdit from "./addEdit.vue";
import dict from "@/views/my-components/xboot/dict";
export default {
  name: "user-manage",
  components: {
    addEdit,
    dict,
  },
  data() {
    return {
      message: "",
      height: 510,
      showUser: false,
      showType: "0",
      openSearch: true,
      loading: true,
      drop: false,
      selectList: [],
      companyTypeList: [], //企业类型列表
      companyScaleList: [], //企业规模列表
      companyNatureList: [], //企业性质列表
      idenTypeList: [], //角色分配
      searchForm: {
        // companyTypeId: '',
        // companyName: '',
        // userNickName: '',
        pageNumber: 1,
        pageSize: 10,
        sort: "createTime",
        order: "desc",
      },
      selectDate: null,
      
      form: {},
      columns: [
        {
          type: "selection",
          width: 60,
          align: "center",
          fixed: "left",
        },
        {
          type: "index",
          width: 60,
          align: "center",
          fixed: "left",
        },
        {
          title: "企业ID",
          key: "id",
          minWidth: 220,
          // sortable: true,
          fixed: "left",
        },
        {
          title: "企业/商铺名称",
          key: "companyName",
          minWidth: 240,
          render: (h, params) => {
            let nodeTxt = h(
              "a",
              {
                on: {
                  click: () => {
                  
                    let [companyNatureObj] = this.companyNatureList.filter(item => {
                      return item.id == params.row.companyTypeId
                    })
                     let [compancaleObj] = this.companyScaleList.filter(item => {
                      return item.id == params.row.companyScaleId
                    })
                     let [companyTypeObj] = this.companyTypeList.filter(item => {
                      return item.id == params.row.companyIndustryId
                    })
                    let obj = {
                      companyNatureObj: companyNatureObj.title,
                      compancaleObj: compancaleObj.title,
                      companyTypeObj: companyTypeObj.title,
                      ...params.row
                    }
                    //目前餐饮和培训为商铺
                    if(params.row.companyTypeId == '1386361312371675137' || params.row.companyTypeId == '1386361332932153345') {
                      //跳转商铺详情
                      localStorage.setItem("companyId",obj.id)
                      this.$router.push({name: 'shop_info', params: obj})
                      return
                    }
                    //跳转企业详情
                    localStorage.setItem("companyId",obj.id)
                    this.$router.push({name: 'company_info',params: obj})
                  }
                }
              },
              params.row.companyName
            )
            return h('div', [nodeTxt])

          }
        },
        {
          title: "企业/商铺管理员",
          key: "user",
          width: 145,
          // align: "center",
          slot: "nickname"
        },
        {
          title: "常用联系方式",
          key: "mobile",
          minWidth: 125,
          slot: "mobile"
          // sortable: true,
        },
        {
          title: "企业/商铺人数",
          key: "count",
          width: 135,
          align: "center",
          // sortable: true,
        },
        {
          title: "剩余企业云打印积分",
          key: "companyPrintIntegral",
          width: 180,
          align: "center",
        },
        {
          title: "剩余企业共享会议积分",
          key: "companyMeetIntegral",
          width: 180,
          align: "center",
        },
        {
          title: "企业性质",
          key: "companyTypeId",
          align: "center",
          width: 110,
          render: (h, params) => {
            let companyTypeIdObj = this.companyNatureList.filter(item => {
              return item.id == params.row.companyTypeId
            })
            let txt = ''
            if(companyTypeIdObj.length>0) {
              txt = companyTypeIdObj[0].title
            }
            return h("div", txt)
          },
        },
        {
          title: "状态",
          key: "status",
          align: "center",
          width: 100,
          render: (h, params) => {
            if (params.row.status == 0) {
              return h("div", [
                h("Badge", {
                  props: {
                    status: "success",
                    text: "启用",
                  },
                }),
              ]);
            } else if (params.row.status == -1) {
              return h("div", [
                h("Badge", {
                  props: {
                    status: "error",
                    text: "禁用",
                  },
                }),
              ]);
            }
          },
          filters: [
            {
              label: "启用",
              value: 0,
            },
            {
              label: "禁用",
              value: -1,
            },
          ],
          filterMultiple: false,
          filterRemote: (e) => {
            let v = "";
            if (e.length > 0) {
              v = e[0];
            }
            this.searchForm.status = v;
            this.searchForm.pageNumber = 1;
            this.getDataList();
          },
        },
        {
          title: "操作",
          key: "action",
          width: 120,
          align: "center",
          fixed: "right",
          render: (h, params) => {
            let enableOrDisable = "";
            if (params.row.status == 0) {
              enableOrDisable = h(
                "a",
                {
                  on: {
                    click: () => {
                      this.chageable(params.row, -1);
                    },
                  },
                },
                "禁用"
              );
            } else {
              enableOrDisable = h(
                "a",
                {
                  on: {
                    click: () => {
                      this.chageable(params.row, 0);
                    },
                  },
                },
                "启用"
              );
            }
            return h("div", [
              enableOrDisable,
              h("Divider", {
                props: {
                  type: "vertical",
                },
              }),
              h(
                "a",
                {
                  on: {
                    click: () => {
                      this.edit(params.row)
                    },
                  },
                },
                "修改"
              ),
               h("Divider", {
                props: {
                  type: "vertical",
                },
              }),
              h(
                "a",
                {
                  on: {
                    click: () => {
                      this.accountDetail(params.row)
                    },
                  },
                },
                "员工消费明细"
              ),
            ]);
          },
        },
      ],
      data: [],
      exportData: [],
      total: 0,
    };
  },
  methods: {
    accountDetail(item){
      localStorage.setItem("companyId",item.id)
      this.$router.push({
        path:"/organizational/accountConslum",
        name:"accountConslum",
      })
    },
    init() {
      this.getDataList();
      this.getCompanyScaleData()
      this.getFindAllData()
      this.getCompanyTypeData()
      this.getScParkRoleAllData()
    },
    edit(v) {
      // 转换null为""
      for (let attr in v) {
        if (v[attr] == null) {
          v[attr] = "";
        }
      }
      let str = JSON.stringify(v);
      let data = JSON.parse(str);
      this.form = data;
      this.showType = "1";
      this.showUser = true;
    },
    handleSelectDep(v) {
      this.searchForm.departmentId = v;
    },
    changePage(v) {
      this.searchForm.pageNumber = v;
      this.getDataList();
      this.clearSelectAll();
    },
    changePageSize(v) {
      this.searchForm.pageSize = v;
      this.getDataList();
    },
    selectDateRange(v) {
      if (v) {
        this.searchForm.startDate = v[0];
        this.searchForm.endDate = v[1];
      }
    },
    getDataList() {
      this.loading = true;
      getAllCompany(this.searchForm).then((res) => {
        this.loading = false;
        if (res.success) {
          this.data = res.result.content;
          this.total = res.result.totalElements;
          if (this.data.length == 0 && this.searchForm.pageNumber > 1) {
            this.searchForm.pageNumber -= 1;
            this.getDataList();
          }
        }
      });
    },
    handleSearch() {
      this.searchForm.pageNumber = 1;
      this.searchForm.pageSize = 10;
      this.getDataList();
    },
    handleReset() {
      // this.$refs.searchForm.resetFields();
      this.searchForm.pageNumber = 1;
      this.searchForm.pageSize = 10;
      this.selectDate = null;
      this.searchForm.startDate = "";
      this.searchForm.endDate = "";
      this.searchForm.companyTypeId = "";
      this.searchForm.companyName = "";
      this.searchForm.userNickName = "";
      this.searchForm.id = "";
      
      this.$refs.dep.clearSelect();
      this.searchForm.departmentId = "";
      // 重新加载数据
      this.getDataList();
    },
    changeSort(e) {
      this.searchForm.sort = e.key;
      this.searchForm.order = e.order;
      if (e.order == "normal") {
        this.searchForm.order = "";
      }
      this.getDataList();
    },
    
    add() {
      this.showType = "2";
      this.showUser = true;
    },
    chageable(v, status) {
      let statusTxt = status == 0 ? '启用' : '禁用'
      this.$Modal.confirm({
        title: `确认${statusTxt}`,
        content: `您确认要${statusTxt}` + v.companyName + " ?",
        loading: true,
        onOk: () => {
          scCompanyUpdStatusById({id:v.id, status}).then((res) => {
            this.$Modal.remove();
            if (res.success) {
              this.$Message.success("操作成功");
              this.getDataList();
            }
          });
        },
      });
    },
    remove(v) {
      this.$Modal.confirm({
        title: "确认删除",
        content: "您确认要删除用户 " + v.username + " ?",
        loading: true,
        onOk: () => {
          deleteUser({ ids: v.id }).then((res) => {
            this.$Modal.remove();
            if (res.success) {
              this.clearSelectAll();
              this.$Message.success("删除成功");
              this.getDataList();
            }
          });
        },
      });
    },
    showSelect(e) {
      this.exportData = e;
      this.selectList = e;
    },
    clearSelectAll() {
      this.$refs.table.selectAll(false);
    },
    getCompanyScaleData() {
      getCompanyScale().then(res => {
        if (res.success) {
          this.companyScaleList = res.result;
        }
      })
    },
    getFindAllData() {
      getFindAll().then(res => {
        if (res.success) {
          this.companyNatureList = res.result;
        }
      })
    },
    getCompanyTypeData() {
      getCompanyType().then(res => {
        if (res.success) {
          this.companyTypeList = res.result;
        }
      })
    },
    getScParkRoleAllData() {
      getScParkRoleAll().then(res => {
        if (res.success) {
          this.idenTypeList = res.result.filter(item => {
            return item.parkRoleType == '2'
          });
        }
      })
    },
  },
  created() {
    getFindAll().then(res => {
      if (res.success) {
        this.companyNatureList = res.result
      }
    })
  },
  mounted() {
    // 计算高度
    this.height = Number(document.documentElement.clientHeight - 230);
    this.init();
  },
};
</script>